package com.example.wangzh.developkit.utils;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.example.wangzh.developkit.base.MyApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

public class PackageUtil {
    /**
     * 得到上下文
     *
     * @return
     */
    public static Context getContext() {
        return MyApplication.getInstance().getApplicationContext();
    }

    /**
     * 得到应用程序的包名
     *
     * @return
     */
    public static String getPackageName() {
        return getContext().getPackageName();
    }

    /**
     * 获取应用包相关信息
     *
     * @return
     */
    public static PackageInfo getPackgeInfo() {
        PackageManager packageManager = getContext().getPackageManager();
        PackageInfo packageInfo = null;
        try {
            packageInfo = packageManager.getPackageInfo(getContext().getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
        }
        return packageInfo;
    }

    /**
     * 获得应用版本名
     */
    public static String getVersionName() {
        PackageInfo packageInfo = getPackgeInfo();
        if (null == packageInfo) {
            return "";
        } else {
            return packageInfo.versionName;
        }
    }

    /**
     * 获得应用版本号
     */
    public static int getVersionCode() {
        PackageInfo packageInfo = getPackgeInfo();
        if (null == packageInfo) {
            return -1;
        } else {
            return packageInfo.versionCode;
        }
    }

    /**
     *
     */
    public static String getPackageChannel() {
        String packageChannel = null;
        PackageManager localPackageManager = getContext().getPackageManager();
        try {
            ApplicationInfo localApplicationInfo = localPackageManager
                    .getApplicationInfo(getContext().getPackageName(), PackageManager.GET_META_DATA);
            if (localApplicationInfo != null) {
                packageChannel = localApplicationInfo.metaData.getString("UMENG_CHANNEL");
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packageChannel;
    }

    public static String readAssert(Context context, String fileName) {
        String jsonString = "";
        String resultString = "";
        try {
            InputStream inputStream = context.getResources().getAssets().open(fileName);
            byte[] buffer = new byte[inputStream.available()];
            inputStream.read(buffer);
            resultString = new String(buffer, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultString;
    }

    /**
     * drawable转为file
     *
     * @param mContext
     * @param drawableId drawable的ID
     * @param fileName   转换后的文件名
     * @return
     */
    public static File drawableToFile(Context mContext, int drawableId, String fileName) {
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), drawableId);
        String defaultPath = mContext.getFilesDir()
                .getAbsolutePath() + "/default";
        File file = new File(defaultPath);
        file.mkdirs();
        String defaultImgPath = defaultPath + "/" + fileName;
        file = new File(defaultImgPath);
        try {
            file.createNewFile();
            FileOutputStream fOut = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            fOut.flush();
            fOut.close();
            return file;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file;
    }
}
